package com.token.sysflow.core;

import com.token.sql.IntegrateSql;
import com.token.sysflow.exception.DraftPowerException;
import com.token.sysflow.exception.ErrorCodeEnum;
import com.token.sysflow.exception.FlowException;
import com.token.sysflow.impl.AbstractFlowHandlerImpl;
import com.token.sysflow.impl.LoadFlowStepImpl;

/**
 * 加载进入
 * @author chenjianxiong 2020/4/13
 */
public class LoadFlowStep extends AbstractFlowCore {

    public LoadFlowStep(IntegrateSql f, String keyUnid) throws Exception {
        super(f, keyUnid);
    }

    public LoadFlowStep(IntegrateSql f) throws Exception{
        super(f);
    }


    /**
     * 执行
     * @param flowHandler
     * @throws Exception
     */
    public void execute(AbstractFlowHandlerImpl flowHandler) throws Exception{
        LoadFlowStepImpl loadFlowStep = (LoadFlowStepImpl) flowHandler;
        int type = loadFlowStep.getLoadType();
        if(type == LoadFlowStepImpl.TYPE_FIRST) {

            this.setAppInfoByAppKeyId(loadFlowStep.getAppKeyId());

            //判断默认流程是否为空
            if ("".equals(appInfo.getDefaultFlowId())) {
                throw new FlowException(ErrorCodeEnum.APP_DEFFLOW_ERROR);
            }

            //设置流程
            this.setFromFlowById(appInfo.getDefaultFlowId());

            //设置环节
            this.setFromStep(this.getFromFlow().getStartStep());

            //判断流程是否激活
            if (!this.getFromFlow().isActive()) {
                throw new FlowException(ErrorCodeEnum.FLOW_NOACTIVE);
            }

            //TODO 流程的使用权限是否还有必要？

            //TODO 流程的查看权限是否还有必要存在？

            //检查起草权限
            if(!this.getFromFlow().checkDraftPower()){
                throw new DraftPowerException();
            }

            //初始化起草人、申请人、当前人。
            initUserInfo(loadFlowStep.getActionUser(),loadFlowStep.getApplyer(),flowHandler.getActionUser());

        }else if(type == LoadFlowStepImpl.TYPE_APPROVAL){

            //初始化起草人、申请人、当前人。
            initUserInfo(approveProcess.getCreater(),approveProcess.getApplyer(),flowHandler.getActionUser());
        }


    }
}
